import numpy as np
from scipy.signal import correlate
from Homomorphic_Envelope_with_Hilbert import Homomorphic_Envelope_with_Hilbert

def getHeartRateSchmidt(audio_data, Fs):
    if audio_data.ndim != 1:
        raise ValueError("Audio data must be a 1D array.")

    homomorphic_envelope = Homomorphic_Envelope_with_Hilbert(audio_data, Fs)

    y = homomorphic_envelope - np.mean(homomorphic_envelope)
    c = correlate(y, y, mode='full', method='auto')
    signal_autocorrelation = c[len(homomorphic_envelope)-1:]

    min_index = round(0.333333 * Fs)  # Corresponding to 160 beats per minute
    max_index = round(0.75 * Fs)    # Corresponding to 80 beats per minute

    true_index = np.argmax(signal_autocorrelation[min_index:max_index]) + min_index

    heartRate = 60 / (true_index / Fs)

    return heartRate
